package br.com.site.model.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import br.com.site.model.dao.DAOException;
import br.com.site.model.dao.DAOFactory;
import br.com.site.model.dao.DiscurssaoDAO;

@Entity
public class Discurssao implements CrudBaseEntity<Discurssao, Long>, Auditoria {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id @GeneratedValue
	private Long id;

	@ManyToOne
	private Estande estandeDiscussao;
	
	@Column(columnDefinition="text")
	private String descricao;

	@ManyToOne
	private Usuario donoDiscussao;

	@OneToMany(mappedBy = "discurssao")
	private List<Mensagem> mensagens;

	/* Auditoria */
	@Column(name = "loginUsuario", nullable = false)
	private String loginUsuario;

	@Column(name = "dtCriacao", nullable = false)
	private Date dtCriacao;

	@Column(name = "dtModificacao", nullable = false)
	private Date dtModificacao;

	@Column(name = "ativo", nullable = false)
	private boolean ativo;

	public String getLoginUsuario() {
		return loginUsuario;
	}

	public void setLoginUsuario(String loginUsuario) {
		this.loginUsuario = loginUsuario;
	}

	public Date getDtCriacao() {
		return dtCriacao;
	}

	public void setDtCriacao(Date dtCriacao) {
		this.dtCriacao = dtCriacao;
	}

	public Date getDtModificacao() {
		return dtModificacao;
	}

	public void setDtModificacao(Date dtModificacao) {
		this.dtModificacao = dtModificacao;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public Estande getEstandeDiscussao() {
		return estandeDiscussao;
	}

	public void setEstandeDiscussao(Estande estandeDiscussao) {
		this.estandeDiscussao = estandeDiscussao;
	}

	
	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public boolean equals(Object obj) {
		boolean bool = false;
		if (obj instanceof Discurssao)
			bool = hashCode() == ((Discurssao) obj).hashCode();
		return bool;
	}

	@Override
	public void remove() throws BusinessException {
		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);
			dao.remove(this.id);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}

	}

	@Override
	public void save() throws BusinessException {

		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);
			dao.save(this);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}
	}

	public static Discurssao find(Long oid) {
		DAOFactory factory = DAOFactory.getInstance();
		DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);

		return dao.find(oid);
	}

	public static List<Discurssao> find() {
		DAOFactory factory = DAOFactory.getInstance();
		DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);

		return dao.find();
	}

	public static List<Discurssao> findByCriteria(Discurssao avisoMural) {
		DAOFactory factory = DAOFactory.getInstance();
		DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);

		return dao.findByCriteria(avisoMural);
	}

	public static List<Discurssao> find(int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);

		return dao.find(first, max);
	}

	public static List<Discurssao> findByCriteria(Discurssao avisoMural,
			int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		DiscurssaoDAO dao = factory.getDao(DiscurssaoDAO.class);

		return dao.findByCriteria(avisoMural, first, max);

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Usuario getDonoDiscussao() {
		return donoDiscussao;
	}

	public void setDonoDiscussao(Usuario donoDiscurssao) {
		this.donoDiscussao = donoDiscurssao;
	}

	public List<Mensagem> getMensagens() {
		return mensagens;
	}

	public void setMensagens(List<Mensagem> mensagens) {
		this.mensagens = mensagens;
	}

}
